package adv10;

import java.io.File;
import java.io.FileWriter;
import java.util.Scanner;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class DelXML {

	public static void main(String[] args) throws Exception {
		SAXReader re = new SAXReader();
		Document doc = re.read(new File("products.xml"));
		Scanner in = new Scanner(System.in);
		System.out.println("请输入要删除的商品号：");
		String itemNo = in.next();
		String str = String.format("/shop/product[itemno=%s]", itemNo);
		Element e = (Element) doc.selectSingleNode(str);
		if (e == null) {
			System.out.println("没有此商品");
			return;
		}
		e.getParent().remove(e);// !!!!!
		OutputFormat f = OutputFormat.createPrettyPrint();
		f.setEncoding("utf-8");
		XMLWriter xm = new XMLWriter(new FileWriter("products.xml"), f);
		xm.write(doc);
		xm.close();
	}

}
